from PySide6.QtWidgets import QApplication, QWidget
from Ui_Converter import Ui_Form


class CoverterWindow(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.lengthVar = {
            "米": 100, "厘米": 1, "分米": 10, "千米": 100000, "里": 50000, "毫米": 0.1, "微米": 0.0001, "纳米": 0.0000001
        }
        self.weightVar = {
            "千克": 1000, "克": 1, "斤": 500, "吨": 1000000, "毫克": 0.001, "微克": 0.000001
        }
        self.timeVar = {
             "秒": 1/60, "分钟": 1, "小时": 60, "天": 24*60, "周": 24*60*7, "毫秒": 1/60000
        }
        self.velocityVar = {
            "米/秒": 3.6, "公里/小时": 1
        }
        self.areaVar = {
            "平方米": 10000, "平方厘米": 1, "平方分米": 100, "平方毫米": 0.01,"公顷": 100000000, "平方千米": 10000000000
        }
        self.volumeVar = {
            "立方米": 1000000, "立方厘米": 1, "立方分米": 1000, "升": 1000, "立方毫米": 0.001, "立方千米": 1000000000000000
        }
        self.frequencyVar = {
            "赫兹": 1, "毫赫": 0.001, "千赫兹": 1000, "微毫赫": 0.000001, "兆赫": 1000000, "千兆赫": 1000000000
        }
        self.TypeDict = {
            "长度": self.lengthVar,
            "质量": self.weightVar,
            "时间": self.timeVar,
            "速度": self.velocityVar,
            "面积": self.areaVar,
            "体积": self.volumeVar,
            "频率": self.frequencyVar
        }
        self.dataTypeComboBox.addItems(self.TypeDict.keys())
        self.oneComboBox.addItems(self.lengthVar.keys())
        self.twoComboBox.addItems(self.lengthVar.keys())
        self.oneInputEditLine.setText("1")
        self.calc()
        self.bind()


    def bind(self):
        self.dataTypeComboBox.currentTextChanged.connect(self.typeChanged)
        self.pushButton.clicked.connect(self.calc)

    def calc(self):
        bigType = self.dataTypeComboBox.currentText()
        value = self.oneInputEditLine.text()
        if value == '':
            return
        
        currentType = self.oneComboBox.currentText()
        transType = self.twoComboBox.currentText()
        standardization = float(value) * self.TypeDict[bigType][currentType]
        result = standardization / self.TypeDict[bigType][transType]
        self.originNumLabel.setText(f'{value} {currentType} =')
        self.transNumLabel.setText(f'{result} {transType}')
        self.twoInputEditLine.setText(str(result))

    def typeChanged(self, text):
        self.oneComboBox.clear()
        self.twoComboBox.clear()
        self.oneComboBox.addItems(self.TypeDict[text].keys())
        self.twoComboBox.addItems(self.TypeDict[text].keys())
        self.calc()

if __name__ == "__main__":
    app = QApplication([])
    window = CoverterWindow()
    window.show()
    app.exec()